package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.other.CommonEvent;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.widget.SelectDialog;
import com.google.gson.Gson;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListActivity;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.QuotationListBean;
import com.tianhui.consignortwofei.mvp.model.enty.QuoationPaiHaoBean;
import com.tianhui.consignortwofei.mvp.ui.adapter.PaiHaoSelectListAdapter;
import com.tianhui.consignortwofei.other.EventWhat;

import org.greenrobot.eventbus.EventBus;
import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/8/10
 * @description
 */
public class PaiHaoSelectActivity extends BaseDataListActivity {


    @BindView(R.id.layout_search_editText)
    AutoCompleteTextView searchEditText;

    @BindView(R.id.ll_search_body)
    LinearLayout ll_search_body;



    private OrderModel driverModel;

    private String mSearchKey;
    private boolean firstGo=true;
    private int lookType;

    private SelectDialog mCargoRightsSelectDialog;
    private List<QuotationListBean.QuotationRecords> dataList =new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_paihao_select_list;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }


    @Override
    public RecyclerArrayAdapter createAdapter() {
//        return new QuotationDetailItemTwoAdapter(getContext(),dataList,lookType);

        return new PaiHaoSelectListAdapter(getContext(),lookType);
    }

//    private void setResult(DriverInfo driverInfo) {
//        Intent intent = new Intent();
//        intent.putExtra(KeyValue.KEY_NAME, driverInfo.drivername);
//        intent.putExtra(KeyValue.KEY_PHONE, driverInfo.drivertel);
//        intent.putExtra(KeyValue.KEY_VEHICLE_NUMBER, driverInfo.defaultvehiclenum);
//        setResult(KeyValue.KEY_RESULT_CODE, intent);
//        finish();
//    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                QuoationPaiHaoBean paiHaoBean = (QuoationPaiHaoBean) mAdapter.getAllData().get(position);
//                goQuotationDetail(position);
                EventBus.getDefault().post(paiHaoBean);
                finish();
            }
        });

//        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
//            @Override
//            public void onItemChildClick(View view, int position) {
//                switch (view.getId()){
//                    case R.id.tv_xiugai:
//                        goQuotationDetail(position);
//                        break;
//                    case R.id.tv_shanchu:
//                        deleteDiaLog(position);
//
//                        break;
//                }
//            }
//        });

    }




    private void goQuotationDetail(int position) {
        QuotationListBean.QuotationRecords driverInfo = (QuotationListBean.QuotationRecords) mAdapter.getAllData().get(position);
        Gson gson=new Gson();
        String json = gson.toJson(driverInfo);
        Bundle bundle=new Bundle();
        bundle.putString("quatationDetail",json);
        bundle.putInt("lookType",lookType);
        openActivity(AddQuotationDetailActivity.class, bundle);

    }

    @Override
    public void requestDataList(int page, boolean isDialog) {
        mSearchKey = searchEditText.getText().toString();
//        if (checkSearchKey(mSearchKey)) {

        requestDriverList(mSearchKey, isDialog,page);

//        } else {
//            stopRefresh();
//        }
    }

    @Override
    public void initData() {
        setTitle("牌号选择");


        driverModel=new OrderModel();

        searchEditText.setHint("请输牌号");

    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }





    @OnClick(R.id.layout_search_textView)
    public void searchDriverDetail() {
        mSearchKey = searchEditText.getText().toString();
//        if (checkSearchKey(mSearchKey)) {
            setRefresh(true);
            requestDriverList(mSearchKey, true,1);
//        }
    }




//    private boolean checkSearchKey(String searchKey) {
//        if (TextUtils.isEmpty(searchKey)) {
//            showEmptyView();
//        } else if (CheckUtil.isMobileNO(mSearchKey)) {
//            return true;
//        } else {
//            showToast("请输入正确的手机号");
//        }
//        return false;
//    }


    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);

        switch (EventWhat.valueOf(event.getWhat())) {
            case EVENT_REFRESH_BAOJIA_LIST:
                setRefresh(true);
                requestDataList(1,true);
                break;
        }
    }

    public void requestDriverList(String paihao, boolean isDialog,int page) {

        Map<String, String> map = new HashMap<>();

        map.put("name", paihao);//牌号
//        map.put("warehouse", kufang);//库房
//        map.put("ownership", huoquan);//货权

        driverModel.getPaiHaoType(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<QuoationPaiHaoBean>>>() {
            @Override
            public void onNext(CommonResponse<List<QuoationPaiHaoBean>> commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        List<QuoationPaiHaoBean> list = commonResponse.getData();

                        if (list != null && list.size() > 0) {

                                showDataList(list);

                        }

                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

                showToast(e.message);
            }
        });


//        driverModel.getOnlyLookNewList(this, map, isDialog, bindLifecycle(), new ObserverResponseListener<CommonResponse<QuotationListBean>>() {
//            @Override
//            public void onNext(CommonResponse<QuotationListBean> commonResponse) {
//                if (!isFinishing() && !isDestroyed()) {
//                    if (commonResponse.getCode() == 20000) {
//                        List<QuotationListBean.QuotationRecords> list = commonResponse.getData().getRecords();
////                        List<QuotationListBean.QuotationRecords> resultList = new ArrayList<>();
//                        dataList.addAll(list);
//
//                        if (list != null && list.size() > 0) {
////                            setPageInfo(1, 1);
////                            for (DriverInfo info : list) {
////                                if (info.drivertel.equals(phone)) {
////                                    resultList.add(info);
////                                }
////                            }
////                            if (list.size() > 0) {
//                                showDataList(list);
////                            } else {
////                                showEmptyView();
////                            }
//
//                        }
//                        else {
//                            showEmptyView();
//                        }
//
//                    } else {
//                        showToast(commonResponse.getMsg());
//                    }
//                }
//            }
//
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                showToast(e.message);
//            }
//        });
    }

    public void deleteDiaLog(int position) {


    }








}